var ResultScene = (function(){

    function ResultScene(){
        Scene2D.call(this,true);
    }

    ResultScene.prototype = new Scene2D();

    var __proto = ResultScene.prototype;
    __proto.initScene = function(node){
        // 适配
        this.bg.height += offH;
        this.bg.y -= offH/2;
        //this._node.y += offH/2;
        // 返回首页
        this.btnHome.on(Laya.Event.MOUSE_DOWN, this, this.goBack);
        // 再玩一次
        this.btnReplay.on(Laya.Event.MOUSE_DOWN, this, this.goGame);
        // 发起挑战
        this.btnChanllenge.on(Laya.Event.MOUSE_DOWN, this, this.goShare);
        // this.btn_begin_t.font = 'mini';
        // this.his_score.font = 'mini';
        // Tool.log(this.btn_begin_t);

        this._stackLength = 0;
        // if(Config.channelID == 102){
        //     this.score.y += 100
        //     this.score.fontSize -= 60
        //     this.his_score.y += 200
        //     this.his_score.fontSize -= 60
        // }
        BaseData.initSceneGameData(this);
    };

    //返回首页
    __proto.goBack = function(){
        globeReset();
        //播放背景音乐
        SceneManager.toggleAction("return_home");
    };
    //再玩一次
    __proto.goGame = function(){
        //播放背景音乐
        GameSceneObj.resetGame();
        SceneManager.toggleAction("game_again");
    };
    //分享
    __proto.goShare = function(){
        var randomTex = [
            "快来挑战我把!",
            "谁能与我一战!",
            "还有谁???",
            "我不是针对谁，我是说在做的各位...",
        ]
        var shareTitle = "我在球球弹弹中获得了 "+GameSceneObj.score+" 分，"+randomTex[Math.floor(Math.random()*randomTex.length)]
        if(Config.channelID == 1){
            WxApp.onShare(shareTitle);
        }
        if(Config.channelID == 2){
            var shareInfo = {
                summary:shareTitle,          //QQ聊天消息标题
                picUrl:"https://oss.leyungame.com/qqttmwx/"+Config.version+"/wxshare/1.jpg",               //QQ聊天消息图片
                localPicPath:"GameRes://qqtt_ewm.png",   //分享至空间、微信、朋友圈时需要的图。（选填，若无该字段，系统使用游戏对应的二维码）
                gameName:"球球弹弹"          //游戏名，暂用与生成二维码
            };
            BK.QQ.share(shareInfo, function (retCode, shareDest, isFirstShare) {});
        }
    };  
    __proto.onActive = function(from){
        Laya.timer.frameOnce(3, this, function () {
            //停止背景音乐
            Audio.stopBg()
            Audio.playVoice("Audios/gameFail.ogg",1);
        });
        var hisScore = BaseData.getGameScore(Config.gameScoreKey);
        if(Config.isAd){
            if(Config.channelID == 101) {
                AdUtil.hideBanner(true);
            }
            // 插屏广告出现的概率
            AdUtil.showInterstitialAd();
        }

        if(Config.channelID == 102){
            this.scoreTitle.text = Tool.insertSpace("本次得分");
            this.score.text = Tool.insertSpace(GameSceneObj.score);
            this.his_score.text = Tool.insertSpace("历 史 最 高 分 :" + hisScore);
        }else{
            this.scoreTitle.text = Tool.insertSpace("本次得分");
            this.score.text = GameSceneObj.score;
            this.his_score.text = "历 史 最 高 分 :" + hisScore;
        }
        if(GameSceneObj.score > hisScore){
            BaseData.saveGameScore(GameSceneObj.score);
        }
        if(Config.channelID == 1){
            var openDataContext = wx.getOpenDataContext();
            //主域发起事件
            openDataContext.postMessage({
                action: "GameRank",
            });

            //微信上存储数据
            openDataContext.postMessage({
                action: "Submit",
                score : GameSceneObj.score,
            });
            if(!this.sharedCanvas){
                //获取共享的canvas
                this.sharedCanvas = Laya.Browser.window.sharedCanvas;
                //设置canvas的属性
                this.sharedCanvas.width = this.share.width;
                this.sharedCanvas.height = this.share.height;
                //渲染接通
                this.shareTex = new Laya.Texture(this.sharedCanvas);
                //update
                
            }
            this.scheCount = 0;
            Laya.timer.loop(2000, this, this.drawWxCanva);

            //延迟调用
            Laya.timer.once(2000, this, function(){
                //查看全部排行
                this.go.on(Laya.Event.MOUSE_DOWN, HomeSceneObj, HomeSceneObj.goRank);
            });
       }else if(Config.channelID == 101) {
            this.btnChanllenge.visible = false;
            this.rankList.visible = false;
       }else{
            this.btnChanllenge.visible = false;
            this.rankList.visible = false;
            this.btnReplay.x = (Config.GameWidth - this.btnReplay.width)/2;
            this.btnReplay.y = (Config.GameHeight - this.btnReplay.height)/2 + 300
            this.scoreList.y = 400;
       }
       if(Config.channelID == 2){
            this.btnReplay.x = (Config.GameWidth - this.btnReplay.width)/2;
            this.btnReplay.y = 1100;
            this.btnChanllenge.y = 1300;
            this.btnChanllenge.visible = true
            this.uploadQQRankData();
        }

        /**
         * oppo原生banner广告
         */
        if(Config.channelID == 101) {
            Laya.timer.once(1000, this, this.onNativeBanner);
        }
    };

    __proto.onInActive = function(){
        Laya.timer.clearAll(this);

    };
    __proto.uploadQQRankData = function () {
        if(typeof BK == "undefined"){
            return
        }
        var data = {
            userData: [
                {
                    openId: GameStatusInfo.openId,
                    startMs: Config.start_game_time.toString(),    //必填。 游戏开始时间。单位为毫秒，<font color=#ff0000>类型必须是字符串</font>
                    endMs: ((new Date()).getTime()).toString(),  //必填。 游戏结束时间。单位为毫秒，<font color=#ff0000>类型必须是字符串</font>
                    scoreInfo: {
                        score: Config.gameScore, //分数，类型必须是整型数
                    },
                },
            ],
            attr: {
                score: {   
                    type: 'rank',
                    order: 1,
                }
            },
        };
        BK.QQ.uploadScoreWithoutRoom(1, data, function(errCode, cmd, data) {
            // 返回错误码信息
            if (errCode !== 0) {
                BK.Script.log(1,1,'上传分数失败!错误码：' + errCode);
            }
        });
    }
    __proto.drawWxCanva = function(){
        if(this.scheCount<3){
           this.scheCount += 1;
           this.share.graphics.drawTexture(this.shareTex,0,0,this.share.width,this.share.height);
        }else{
            Laya.timer.clear(this,this.drawWxCanva);
        }
    };


    /**
     * oppo原生banner广告
     */
    __proto.onNativeBanner = function() {
        Tool.log("-------init oppo ad native ad onNativeBanner-------------------");
        var that = this;
        if(Config.channelID == 101 && Config.isLoadSuccess) {
            // 获取Banner原生广告
            AdUtil.showNativeBanner("nativeBanner", "42799", function(res) {
                // 原生广告获取成功
                Tool.log("-------init oppo ad native ad success-------------------");
                for(var i = 0;i < res.adList.length;i++) {       
                    // 广告标识
                    Tool.log("-------init oppo ad native ad success adId -------", res.adList[i].adId);
                    // 广告标题
                    Tool.log("-------init oppo ad native ad success title -------", res.adList[i].title);
                    // 广告描述
                    Tool.log("-------init oppo ad native ad success desc -------", res.adList[i].desc);
                    // 推广应用的Icon图标
                    Tool.log("-------init oppo ad native ad success iconUrlList -------", res.adList[i].iconUrlList);
                    // 广告图片
                    Tool.log("-------init oppo ad native ad success imgUrlList -------", res.adList[i].imgUrlList);
                    // “广告”标签图片
                    Tool.log("-------init oppo ad native ad success logoUrl -------", res.adList[i].logoUrl);
                    // 点击按钮文本描述
                    Tool.log("-------init oppo ad native ad success clickBtnTxt -------", res.adList[i].clickBtnTxt);
                    // 获取广告类型，取值说明：0：无 1：纯文字 2：图片 3：图文混合 4：视频
                    Tool.log("-------init oppo ad native ad success creativeType -------", res.adList[i].creativeType);
                    // 获取广告点击之后的交互类型，取值说明： 0：无 1：浏览类 2：下载类 3：浏览器（下载中间页广告） 4：打开应用首页 5：打开应用详情页
                    Tool.log("-------init oppo ad native ad success interactionType -------", res.adList[i].interactionType);
                }
                // 目前只有一个
                // 保存原生banner广告的ID
                Config.adBannerId = res.adList[0].adId;
                // 加载原生ICON广告         
                that.native_banner_img.skin = res.adList[0].imgUrlList[0];
                that.native_banner_img_ad.skin = res.adList[0].logoUrl;
                that.native_banner_img.on(Laya.Event.MOUSE_DOWN, that, that.onNativeBannerClick);
            }, function(err) {
                Tool.log("-------init oppo ad native ad onError-------------------", err.code);
                Tool.log("-------init oppo ad native ad onError-------------------", err.msg);
            });

            return;
        }
        Laya.timer.once(1000, this, this.onNativeBanner);
    }

    /**
     * oppo原生BANNER广告曝光
     */
    __proto.onNativeBannerClick = function() {
        Tool.log("-------init oppo ad native ad 原生BANNER广告点击");
        if(Config.channelID == 101) {
            Config.nativeBanner.reportAdShow({
                adId: Config.adBannerId 
            })
            Config.nativeBanner.reportAdClick({
                adId: Config.adBannerId
            })
        }
    }
 
    return ResultScene;
})();